package com.along.utility.common.utils;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;
import java.util.TimeZone;

/**
 * 日期处理公共类
 *
 * @author haojianjian
 *
 */
public class DateUtil {


	private static final String ALL_FORMAT = "yyyy-MM-dd HH:mm:ss";
	private static final String FORMAT = "yyyy-MM-dd";
	private static final String _FORMAT = "yyyyMMdd";
	private static final String FORMAT_ALL = "yyyyMMdd HH:mm:ss";
	private static final  String __FORMAT = "yyyy.MM.dd";
	private static final String TIME_STAMP = "yyyyMMddHHmmssSSS";
	/**
	 * 将毫秒数转换为yyyy-MM-dd HH:mm:ss格式
	 *
	 * @param millis
	 * @return
	 */
	public static String millis2StringHHmmss(long millis) {

		Calendar calendar = Calendar.getInstance();

		calendar.setTimeInMillis(millis);

		SimpleDateFormat sdf = new SimpleDateFormat(ALL_FORMAT);

		return sdf.format(calendar.getTime());

	}

	/**
	 * 将秒数转换为yyyy-MM-dd HH:mm:ss格式
	 *
	 * @param millis
	 * @return
	 */
	public static String seconds2StringHHmmss(long second) {

		Calendar calendar = Calendar.getInstance();

		calendar.setTimeInMillis(second * 1000);

		SimpleDateFormat sdf = new SimpleDateFormat(ALL_FORMAT);

		return sdf.format(calendar.getTime());

	}

	/**
	 * 将秒数转换为Date
	 *
	 * @param second
	 * @return
	 */
	public static Date seconds2Date(long second) {
		Calendar calendar = Calendar.getInstance();
		calendar.setTimeInMillis(second * 1000);
		return calendar.getTime();
	}

    /**
     * yyyy-MM-dd to date
     * @param dateStr
     * @return
     */
    public static Date strs2Date(String dateStr){
    	if (dateStr != null && !"".equals(dateStr)) {
            SimpleDateFormat sdf = new SimpleDateFormat(FORMAT);
            try {
                return sdf.parse(dateStr);
            } catch (Exception e) {
            }
        }
        return null;

    }

    /**
	 * 将日期形转为yyyy-MM-dd 23:01:59形式
	 *
	 * @param date
	 * @return
	 */
	public static String date2StringHHmmss(Date date) {
		if (date != null) {
			SimpleDateFormat sdf = new SimpleDateFormat(ALL_FORMAT);
			return sdf.format(date);
		} else {
			return "";
		}
	}

	/**
	 * yyyyMMddHHmmss to date
	 *
	 * @param dateStr
	 * @return
	 */
	public static Date str2Date(String dateStr) {
		if (dateStr != null && !"".equals(dateStr)) {
			SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
			try {
				return sdf.parse(dateStr);
			} catch (Exception e) {
			}
		}
		return null;

	}

	/**
	 * 将yyyy-MM-dd 23:01:59 类型的字符串转为日期
	 *
	 * @param dateStr
	 * @return
	 */
	public static Date stringHHmmss2Date(String dateStr) {
		if (dateStr != null && !"".equals(dateStr)) {
			SimpleDateFormat sdf = new SimpleDateFormat(ALL_FORMAT);
			try {
				return sdf.parse(dateStr);
			} catch (Exception e) {

			}
		}
		return null;
	}

	/**
	 * 将yyyy-MM-dd 格式的字符串转为日期
	 *
	 * @param dateStr
	 * @return
	 */
	public static Date string2Date(String dateStr) {
		if (dateStr != null && !"".equals(dateStr)) {
			SimpleDateFormat sdf = new SimpleDateFormat(FORMAT);
			try {
				return sdf.parse(dateStr);
			} catch (Exception e) {
			}
		}
		return null;
	}

	/**
	 * 将strin类型的日期转化成Date
	 *
	 * @param dateStr
	 * @return
	 */
	public static Date stringToDateY(String dateStr,String regular) {
		if (dateStr != null && !"".equals(dateStr)) {
			SimpleDateFormat sdf = new SimpleDateFormat(regular);
			try {
				return sdf.parse(dateStr);
			} catch (Exception e) {
			}
		}
		return null;
	}

	/**
	 * 将yyyy.MM.dd 格式的字符串转为日期
	 *
	 * @param dateStr
	 * @return
	 */
	public static Date stringToDate(String dateStr) {
		if (dateStr != null && !"".equals(dateStr)) {
			SimpleDateFormat sdf = new SimpleDateFormat(__FORMAT);
			try {
				return sdf.parse(dateStr);
			} catch (Exception e) {
			}
		}
		return null;
	}


	/**
	 * 将日期形转为yyyy-MM-dd 形式
	 *
	 * @param date
	 * @return
	 */
	public static String date2String(Date date) {
		if (date != null) {
			SimpleDateFormat sdf = new SimpleDateFormat(FORMAT);
			return sdf.format(date);
		} else {
			return "";
		}
	}
    /**
     * 将日期形转为yyyyMMdd 形式
     *
     * @param date
     * @return
     */
    public static String date3String(Date date) {
        if (date != null) {
            SimpleDateFormat sdf = new SimpleDateFormat(_FORMAT);
            return sdf.format(date);
        } else {
            return "";
        }
    }
    /**
     * 将日期形转为yyyyMMdd HH:mm:ss 形式
     *
     * @param date
     * @return
     */
    public static String date2StringFormatAll(Date date) {
    	if (date != null) {
    		SimpleDateFormat sdf = new SimpleDateFormat(FORMAT_ALL);
    		return sdf.format(date);
    	} else {
    		return "";
    	}
    }
    /**
     * 将yyyyMMdd日期形转为 日期
     *
     * @param date
     * @return
     */
    public static Date string3Date(String date) {

        SimpleDateFormat sdf = new SimpleDateFormat(_FORMAT);
        try {
			return sdf.parse(date);
		} catch (ParseException e) {

		}
        return null;
    }
    /**
     * 将yyMMdd日期形转为日期
     *
     * @param date
     * @return
     */
    public static Date string4Date(String date) {

    	SimpleDateFormat sdf = new SimpleDateFormat("yyMMdd");
    	try {
    		return sdf.parse(date);
    	} catch (ParseException e) {

    	}
    	return null;
    }
    /**
     * 将String类型yyyyMMddHHmmss日期转为date日期
     *
     * @param date
     * @return
     */
    public static Date string5Date(String date) {

    	SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
    	try {
    		return sdf.parse(date);
    	} catch (ParseException e) {

    	}
    	return null;
    }
    /**
     * 将yyMMddHHmmss日期形转为日期
     *
     * @param date
     * @return
     */
    public static Date string6Date(String date) {

    	SimpleDateFormat sdf = new SimpleDateFormat("yyMMddHHmmss");
    	try {
    		return sdf.parse(date);
    	} catch (ParseException e) {

    	}
    	return null;
    }
    /**
	 * 将时间戳 转换成yyyy-MM-dd 23:01:59 类型
	 *
	 * @param dateStr
	 * @return
	 */
    public static Date long2Date(long longDate) {
			SimpleDateFormat format =  new SimpleDateFormat(ALL_FORMAT);
			String dDate = format.format(new Date(longDate * 1000L));
			try {
				return format.parse(dDate);
			} catch (Exception e) {

			}
		return null;
	}
    /**
     * 获取指定几天前的日期字符串格式
     * @param before
     * @return
     */
    public static String getDate(int before){
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(new Date());
		calendar.add(Calendar.DAY_OF_MONTH, before);
		Date date = calendar.getTime();
		return new SimpleDateFormat("yyyyMMdd").format(date);
	}
    /**
     * 获取指定几天前的日期字符串格式
     * @param before
     * @return
     */
    public static String getTime(int before){
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(new Date());
		calendar.add(Calendar.DAY_OF_MONTH, before);
		Date date = calendar.getTime();
		return new SimpleDateFormat("yyyyMMddHHmmss").format(date);
	}
    /**
     * 获取指定几天前的日期格式
     * @param before
     * @return
     */
	public static Date getDateBefore(int before){
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(new Date());
		calendar.add(Calendar.DAY_OF_MONTH, before);
		return calendar.getTime();
	}
	 /**
     * 获取指定几天前的对应的年月日
     * @param before
     * @return
     */
	public static String getYMD(int before){
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(new Date());
		calendar.add(Calendar.DAY_OF_MONTH, before);
		int year = calendar.get(Calendar.YEAR);
		int day = calendar.get(Calendar.DATE);
		DateFormat df = new SimpleDateFormat("MMM",Locale.ENGLISH);
		String month = df.format(calendar.getTime());
		return year+","+month+","+day;
	}
	/**
	 * 获取指定年月日前几个月的时间（yyyy-MM-dd）
	 * @param after
	 * @return
	 */
	public static String getMonthAfter(int after){
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(new Date());
		calendar.add(Calendar.MONTH, + after);
		Date time = calendar.getTime();
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		return sdf.format(time);
	}
	/**
	 * 获取yyyy-MM-dd HH:mm:ss
	 * @param date yyyyMMddHHmmss
	 * @return
	 */
	public static String stringDateToString(String date){
		//ALL_FORMAT
		try {
			Date parse = new SimpleDateFormat("yyyyMMddHHmmss").parse(date);
			return date2StringHHmmss(parse);
		} catch (ParseException e) {
		}
		return null;
	}
	/**
	 * 获取yyyyMMddHHmmss
	 * @param date yyyy-MM-dd HH:mm:ss
	 * @return
	 */
	public static String stringDateToString1(String date){
		//ALL_FORMAT
		try {
			Date parse = new SimpleDateFormat(ALL_FORMAT).parse(date);
			SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
			return sdf.format(parse);
		} catch (ParseException e) {
		}
		return null;
	}
	/**
	 * UTC时间戳转北京时间
	 * @param longDate
	 * @return
	 */
	public static Date getUTC2Date(long longDate){
		Date long2Date = long2Date(longDate);
		String date = date2StringHHmmss(long2Date);
		SimpleDateFormat dateFormat = new SimpleDateFormat(ALL_FORMAT);
		 dateFormat.setTimeZone(TimeZone.getTimeZone("UTC+:08:00"));
		 try {
	            Date dateTmp = dateFormat.parse(date);
	            return dateTmp;

	         } catch (ParseException e) {
	        }
		return null;
	}

	/**
	 * 获取月份的第一天
	 * @param date
	 * @return
	 */
	public static String getMonthFirstDay(Date date){
		if (date != null) {
			 SimpleDateFormat format = new SimpleDateFormat("yyyyMMdd");
			Calendar calendar = Calendar.getInstance();
			calendar.setTime(date);
			calendar.add(Calendar.MONTH, 0);
			calendar.set(Calendar.DAY_OF_MONTH, 1);
			return format.format(calendar.getTime());
		}else{
			return "";
		}
	}

	/**
	 * 获取月份的最后一天
	 * @param date
	 * @return
	 */
	public static String getMonthLastDay(Date date){
		if (date != null) {
			SimpleDateFormat format = new SimpleDateFormat("yyyyMMdd");
			Calendar calendar = Calendar.getInstance();
			calendar.setTime(date);
			calendar.add(Calendar.MONTH, 1);
			calendar.set(Calendar.DAY_OF_MONTH, -1);
			return format.format(calendar.getTime());
		}else{
			return "";
		}
	}

	/**
     * 将日期形转为yyyyMMddHHmmss 形式
     *
     * @param date
     * @return
     */
    public static String date4String(Date date) {
    	if (date != null) {
    		SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
    		return sdf.format(date);
    	} else {
    		return "";
    	}
    }

    /**
     * 将日期形转为HH:mm 形式
     *
     * @param date
     * @return
     */
    public static String date5String(Date date) {
    	if (date != null) {
    		SimpleDateFormat sdf = new SimpleDateFormat("HH:mm");
    		return sdf.format(date);
    	} else {
    		return "";
    	}
    }

    /**
     * 将日期形转为HH:mm:ss 形式
     *
     * @param date
     * @return
     */
    public static String date6String(Date date) {
    	if (date != null) {
    		SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss");
    		return sdf.format(date);
    	} else {
    		return "";
    	}
    }

    /**
     * 将日期形转为yyyyMM 形式
     *
     * @param date
     * @return
     */
    public static String date7String(Date date) {
    	if (date != null) {
    		SimpleDateFormat sdf = new SimpleDateFormat("yyyyMM");
    		return sdf.format(date);
    	} else {
    		return "";
    	}
    }

    /**
     * 将日期形转为MMdd 形式
     *
     * @param date
     * @return
     */
    public static String date8String(Date date) {
    	if (date != null) {
    		SimpleDateFormat sdf = new SimpleDateFormat("MMdd");
    		return sdf.format(date);
    	} else {
    		return "";
    	}
    }

    /**
     * 将日期形转为MM月dd日形式
     *
     * @param date
     * @return
     */
    public static String date9String(Date date) {
    	if (date != null) {
    		SimpleDateFormat sdf = new SimpleDateFormat("M月d日");
    		return sdf.format(date);
    	} else {
    		return "";
    	}
    }

    /**
     * 获取指定几天前的日期格式
     * @param before
     * @return
     */
	public static Date getDateBefore(Date date,int befor){
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		calendar.add(Calendar.DAY_OF_MONTH, befor);
		return calendar.getTime();
	}

	/**
	 * 获取指定几天前的日期格式
	 * @param before
	 * @return
	 */
	public static Date getDateBeforeHour(Date date,int befor){
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		calendar.add(Calendar.HOUR_OF_DAY, befor);
		return calendar.getTime();
	}

	/**
	 * 获取当前时间戳（yyyyMMddHHmmssSSS）
	 * @return
	 */
	public static String getTimeStamp(){
		SimpleDateFormat sdf = new SimpleDateFormat(TIME_STAMP);
		return sdf.format(new Date());
	}

	  /**
     * Java将Unix时间戳转换成指定格式日期字符串
     * @param timestampString 时间戳 如："1513051932";
     * @param formats 要格式化的格式 默认："yyyy-MM-dd HH:mm:ss";
     *
     * @return 返回结果 如："2017-12-12 12:12:12";
     */
    public static String timeStamp2Date(String timestampString, String formats) {
    	if (null == formats || "".equals(formats)){
    		formats = ALL_FORMAT;
    	}
    	if(null != timestampString && !"".equals(timestampString)){
    		Long timestamp = Long.parseLong(timestampString) * 1000;
    		String date = new SimpleDateFormat(formats, Locale.CHINA).format(new Date(timestamp));
    		return date;
    	}
    	return "";
    }

	public static Integer getHourBefore(Date date,int before){
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		calendar.add(Calendar.HOUR_OF_DAY, before);
		int hour = calendar.get(Calendar.HOUR_OF_DAY);
		return hour;
	}

    /**
     * 获取指定几月前的日期格式
     * @param before
     * @return
     */
	public static Date getDateBeforeMonth(Date date,int befor){
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		calendar.add(Calendar.MONTH, befor);
		return calendar.getTime();
	}

	/**
	 * 获取指定几分钟前的日期格式
	 * @param before 返回date类型
	 * @return
	 */
	public static Date getDateBeforeMinute(Date date,int befor){
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		calendar.add(Calendar.MINUTE, befor);
		return calendar.getTime();
	}

	/**
	 * 获取指定几分钟前的日期格式
	 * @param before 返回String类型
	 * @return
	 */
	public static String getDateBeforeMinuteStr(Date date,int befor){
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		calendar.add(Calendar.MINUTE, befor);
		return new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(calendar.getTime());
	}
}
